<?php

/**
 * Реализация своего валидатора
 *
 */

class FRM_Validate_Abstract extends Zend_Validate 
{
    /**
     * Даные для валидации
     *
     * @var array
     */
    private $data = array();
    
    /**
     * Масив постоеных валидаторов
     *
     * @var array
     */
    private $validators = array();
    
    /**
     * Назначение даних для валидации
     *
     * @param array $_data
     */
    public function setData($_data){
        $this->data = $_data;
        $this->preValidation();
    }
    
    /**
     * Выбор даних
     *
     * 
     */
    public function getData(){
        return $this->data;
    }
    
    /**
     * 
     * Установка значения по умолчанию
     *
     */
    private function setDefaultValue(&$var,$value=false){
        $var=isset($var)?($var):($value);
    }
    
    /**
     * Привязка даних к валидатору
     * В этом методе строится зендовская валидаци на основе даних ($data) и правил валидации
     *
     */
    private  function preValidation(){
        //TODO;
        //$this->validators = array();
    }
    
    
    /**
     * 
     * Валидация одного елемента
     *
     */
    private function ValidateItem(&$validator,$key){
    	if (!$validator->isValid($this->data[$key]))
			{
				$result=0;
				$this->_messages[$key]=$validator->getMessages();
			}
			else 
			{
				$result=1;
			}
		return $result;
    }
    
    /**
     * Проверка на валидацию всего класса
     *
     * @return boolean
     */
    public function isValidWhole(){
    	foreach ($this->data as $key=>$value)
    	{
    		if (!is_array($this->validators[$key]['type']))
    			$this->validators[$key]['type']=array($this->validators[$key]['type']);
    		foreach ($this->validators[$key]['type'] as $type)
    		{
    			switch ($type) {
	    			case 'Alnum':
	    				setDefaultValue($this->data[$key]['rules']['allowWhiteSpace']);
	    				$validator = new Zend_Validate_Alnum($this->$this->data[$key]['rules']['allowWhiteSpace']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    					
	    				break;
	    				
	    			case 'Alpha':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				setDefaultValue($this->data[$key]['rules']['allowWhiteSpace']);
	    				$validator = new Zend_Validate_Alpha($this->data[$key]['rules']['allowWhiteSpace']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			case 'Barcode':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = Zend_Validate_Barcode($this->data[$key]['rules']['barcodeType']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			case 'Barcode_Ean13':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Barcode_Ean13();
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			case 'Barcode_UpcA':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Barcode_UpcA();
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			case 'Between':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				setDefaultValue($this->data[$key]['rules']['inclusive'],true);
	    				$validator = new Zend_Validate_Between($this->data[$key]['rules']['min'],$this->data[$key]['rules']['max'],$this->data[$key]['rules']['inclusive']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			case 'Ccnum':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Ccnum(); 
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			case 'Date':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validatorChain = new Zend_Validate_Date($this->data[$key]['rules']['format'],$this->data[$key]['rules']['locale']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
					case 'Digits':
						setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Digits();
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
					case 'EmailAddress':
						setDefaultValue($this->data[$key]['rules']['brake']);
						setDefaultValue($this->data[$key]['rules']['validateMx']);
	    				$validator = new Zend_Validate_EmailAddress($this->data[$key]['rules']['allow'],$this->data[$key]['rules']['validateMx']);      					
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
					case 'Exception':
						setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Exception($this->data[$key]['rules']['message'],$this->data[$key]['rules']['code']);      					
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
					case 'Float':
						setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Float();
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
					case 'GreaterThan':
						setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_GreaterThan($this->data[$key]['rules']['min']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
					case 'Hex':
						setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Hex();
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break; 
	    				   		
					case 'Hostname':
						setDefaultValue($this->data[$key]['rules']['brake']);
						setDefaultValue($this->data[$key]['rules']['validateIdn'],true);
						setDefaultValue($this->data[$key]['rules']['validateTdn'],true);
	    				$validator = new Zend_Validate_Hostname($this->data[$key]['rules']['allow'],$this->data[$key]['rules']['validateIdn'],$this->data[$key]['rules']['validateTid']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;  
	    				
	    			case 'Identical':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validatorChain = new Zend_Validate_Identical($this->data[$key]['rules']['token']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;   	
	    					
	    			case 'InArray':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				setDefaultValue($this->data[$key]['rules']['strict']);
	    				$validatorChain  = new Zend_Validate_InArray($this->data[$key]['rules']['haystack'],$this->data[$key]['rules']['strict']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;   
	    				
	    			case 'Int':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Int();
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break; 
	    				
	    			case 'Ip':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_Ip();
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break; 
	    				
	    			case 'LessThan':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validator = new Zend_Validate_LessThan($this->data[$key]['rules']['max']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			case 'NotEmpty':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validatorChain = new Zend_Validate_NotEmpty();
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			case 'Regex':
	    				setDefaultValue($this->data[$key]['rules']['brake']);
	    				$validatorChain = new Zend_Validate_Regex($this->data[$key]['rules']['pattern']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
					case 'StringLength':
						setDefaultValue($this->data[$key]['rules']['brake']);	
						setDefaultValue($this->data[$key]['rules']['min'],0);
	    				$validatorChain = new Zend_Validate_StringLength($this->data[$key]['rules']['min'],$this->data[$key]['rules']['max']);
	    				$validator = ValidateItem($validator,$key);
	    				$result[$key]=$this->ValidateItem($validator,$key);
	    				break;
	    				
	    			default:
	    				break;
	    		}
    		}		
    	}
    
    	/*$validatorChain = new Zend_Validate();
    	foreach ($this->validators as $itemValidator) {
    		switch ($itemValidator) {
    			case 'Alnum':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				setDefaultValue($this->data[$key]['rules']['allowWhiteSpace']);
    				$validatorChain ->addValidator(new Zend_Validate_Alnum($this->data[$key]['rules']['allowWhiteSpace']),$this->data[$key]['rules']['brake']);
    				break;
    				
    			case 'Alpha':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				setDefaultValue($this->data[$key]['rules']['allowWhiteSpace']);
    				$validatorChain ->addValidator(new Zend_Validate_Alpha($this->data[$key]['rules']['allowWhiteSpace']),$this->data[$key]['rules']['brake']);
    				break;
    				
    			case 'Barcode':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Barcode($this->data[$key]['rules']['barcodeType']),$this->data[$key]['rules']['brake']);
    				break;
    				
    			case 'Barcode_Ean13':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Barcode_Ean13(),$this->data[$key]['rules']['brake']);
    				break;
    				
    			case 'Barcode_UpcA':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Barcode_UpcA(),$this->data[$key]['rules']['brake']);
    				break;
    				
    			case 'Between':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				setDefaultValue($this->data[$key]['rules']['inclusive'],true);
    				$validatorChain ->addValidator(new Zend_Validate_Between($this->data[$key]['rules']['min'],$this->data[$key]['rules']['max'],$this->data[$key]['rules']['inclusive']),$this->data[$key]['rules']['brake']);
    				break;
    				
    			case 'Ccnum':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Ccnum(),$this->data[$key]['rules']['brake']); 
    				break;
    				
    			case 'Date':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Date($this->data[$key]['rules']['format'],$this->data[$key]['rules']['locale']),$this->data[$key]['rules']['brake']);
    				break;
    				
				case 'Digits':
					setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Digits(),$this->data[$key]['rules']['brake']);   
    				break;
    				
				case 'EmailAddress':
					setDefaultValue($this->data[$key]['rules']['brake']);
					setDefaultValue($this->data[$key]['rules']['validateMx']);
    				$validatorChain ->addValidator(new Zend_Validate_EmailAddress($this->data[$key]['rules']['allow'],$this->data[$key]['rules']['validateMx']),$this->data[$key]['rules']['brake']);      					
    				break;
    				
				case 'Exception':
					setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Exception($this->data[$key]['rules']['message'],$this->data[$key]['rules']['code']),$this->data[$key]['rules']['brake']);      					
    				break;
    				
				case 'Float':
					setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Float(),$this->data[$key]['rules']['brake']);      					    				
    				break;
    				
				case 'GreaterThan':
					setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_GreaterThan($this->data[$key]['rules']['min']),$this->data[$key]['rules']['brake']);
    				break;
    				
				case 'Hex':
					setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Hex(),$this->data[$key]['rules']['brake']);
    				break; 
    				   		
				case 'Hostname':
					setDefaultValue($this->data[$key]['rules']['brake']);
					setDefaultValue($this->data[$key]['rules']['validateIdn'],true);
					setDefaultValue($this->data[$key]['rules']['validateTdn'],true);
    				$validatorChain ->addValidator(new Zend_Validate_Hostname($this->data[$key]['rules']['allow'],$this->data[$key]['rules']['validateIdn'],$this->data[$key]['rules']['validateTid']),$this->data[$key]['rules']['brake']);
    				break;  
    				
    			case 'Identical':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Identical($this->data[$key]['rules']['token']),$this->data[$key]['rules']['brake']);
    				break;   	
    					
    			case 'InArray':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				setDefaultValue($this->data[$key]['rules']['strict']);
    				$validatorChain ->addValidator(new Zend_Validate_InArray($this->data[$key]['rules']['haystack'],$this->data[$key]['rules']['strict']),$this->data[$key]['rules']['brake']);
    				break;   
    				
    			case 'Int':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Int(),$this->data[$key]['rules']['brake']);
    				break; 
    				
    			case 'Ip':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Ip(),$this->data[$key]['rules']['brake']);
    				break; 
    				
    			case 'LessThan':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_LessThan($this->data[$key]['rules']['max']),$this->data[$key]['rules']['brake']);
    				break;
    				
    			case 'NotEmpty':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_NotEmpty(),$this->data[$key]['rules']['brake']);
    				break;
    				
    			case 'Regex':
    				setDefaultValue($this->data[$key]['rules']['brake']);
    				$validatorChain ->addValidator(new Zend_Validate_Regex($this->data[$key]['rules']['pattern']),$this->data[$key]['rules']['brake']);
    				break;
    				
				case 'StringLength':
					setDefaultValue($this->data[$key]['rules']['brake']);	
					setDefaultValue($this->data[$key]['rules']['min'],0);
    				$validatorChain ->addValidator(new Zend_Validate_StringLength($this->data[$key]['rules']['min'],$this->data[$key]['rules']['max']),$this->data[$key]['rules']['brake']);
    				break;
    				
    			default:
    				break;
    		}
    	}*/
    	
    	return $result;
    }
    
    /**
     * Получаем масив правил валидации
     *
     * @return array
     */
    public function getValidators(){
        return $this->validators;
    }
    
    /**
     * Назначаем правила валидации
     *
     */
    public function setValidators($validators){
        $this->validators = $validators;
    }
}